package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.workcellinfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.FaultTypeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.FaultType;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.workcellinfo.JsmFaultTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian@cqhilink.com on 2016/12/12.
 * 故障设备类型管理实现类
 */

public class JsmFaultTypeServiceImpl implements JsmFaultTypeService {

    @Autowired
    public FaultTypeMapper faultTypeMapper;

    /**
     * 故障类型列表
     * @param faultType        生产生产实体类型唯一标识
     */
    @Override
    public JSONObject table(FaultType faultType) throws Exception {
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        jsonObject.put("total", faultTypeMapper.queryCount(faultType));
        List<FaultType> faultTypeList = faultTypeMapper.queryFaultList(faultType);
        jsonObject.put("rows", JSON.toJSON(faultTypeList));
        return jsonObject;
    }

    /**
     * 故障类型新增
     *
     * @param faultType 故障类型，
     * @return
     * @throws Exception
     */
    @Override
    public Integer addFaultType(FaultType faultType,Employee employee) throws Exception {
        Integer resInt = 0;
        //Employee employee = employeeService.getLoginEmployee();
        faultType.setModifierId(employee.getEmployeeId());
        faultType.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        if (faultType.getFaultTypeId() == null) {
            faultType.setFaultTypeId(UniqueUtil.uuid());
            faultType.setCreatorId(employee.getEmployeeId());
            faultType.setCreateEtime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            faultType.setStatus(Short.valueOf("0"));
            resInt = faultTypeMapper.insertSelective(faultType);
        } else {
            resInt = faultTypeMapper.updateByPrimaryKeySelective(faultType);
        }
        return resInt;
    }

    /**
     * 故障类型删除功能
     *
     * @param faultTypeIds 故障类型，
     * @return
     * @throws Exception
     */
    @Override
    public Integer delFaultType(Map faultTypeIds) {

        return faultTypeMapper.deleteByPrimaryIds(faultTypeIds);
    }

    /**
     * 查看故障类型
     * @return
     * @throws Exception
     */
    @Override
    public FaultType queryFaultType(String faultTypeId) throws Exception {
        return faultTypeMapper.selectByPrimaryKey(faultTypeId);
    }
}
